package com.yaolonga.dc.core.model;

import com.yaolonga.dc.core.GroupNode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;
import java.util.*;

/**
 * @author yaolong
 * @version V1.0.0
 * @program dynamic-condition
 * @description 查询model
 * @create 2024-06-01 18:09
 **/
@Getter
@Setter
@AllArgsConstructor(staticName = "of")
@NoArgsConstructor
public class SearchModel implements Serializable {
    Integer page;
    Integer pageSize;
    //条件构造ast
    List<GroupNode> ast;
    // 排序
    List<com.yaolonga.dc.core.model.Sort> sorts;
    //返回字段
    protected Set<String> columns = new LinkedHashSet<>();


    public SearchModel(List<GroupNode> ast) {
        this.ast = ast;
        this.page = 1;
        this.pageSize = 10;
    }
    public SearchModel(List<GroupNode> ast, Integer page, Integer pageSize) {
        this.ast = ast;
        this.page = page;
        this.pageSize = pageSize;
    }
    public SearchModel(List<GroupNode> ast, Integer page, Integer pageSize, List<com.yaolonga.dc.core.model.Sort> sorts) {
        this.ast = ast;
        this.page = page;
        this.pageSize = pageSize;
        this.sorts = sorts;
    }
    public SearchModel(List<GroupNode> ast, Integer page, Integer pageSize, List<com.yaolonga.dc.core.model.Sort> sorts, Set<String> columns) {
        this.ast = ast;
        this.page = page;
        this.pageSize = pageSize;
        this.sorts = sorts;
        this.columns = columns;
    }



    public SearchModel page(Integer page) {
        this.page = page;
        return this;
    }
    public SearchModel pageSize(Integer pageSize) {
        this.pageSize = pageSize;
        return this;
    }
    public SearchModel sorts(List<com.yaolonga.dc.core.model.Sort> sorts) {
        this.sorts = sorts;
        return this;
    }

    public SearchModel sort(com.yaolonga.dc.core.model.Sort sort) {
        if (this.sorts == null) {
            this.sorts = new LinkedList<>();
        }
        this.sorts.add(sort);
        return this;
    }

    public SearchModel sort(String field, String value) {
        this.sort(com.yaolonga.dc.core.model.Sort.of(field, value));
        return this;
    }

    public SearchModel sort(String field) {
        this.sort(Sort.of(field));
        return this;
    }

    public SearchModel columns(String... columns) {
        this.columns.addAll(Arrays.asList(columns));
        return this;
    }

    public SearchModel ast(List<GroupNode> ast) {
        this.ast = ast;
        return this;
    }

}

